<?
if (! function_exists('sidebar_build_menu')) {
    /**
     * 构造菜单选项
     *
     * @param  array  $config
     * @return string
     */
    function sidebar_build_menu(array $config, $level = 1) {
        $html = '';
        foreach ($config as $key => $val) {
            if (! $allowed = array_get($val, 'allowed', true)) {
                continue;
            }

            // 图标
            $icon = array_get($val, 'icon', 'arrow-circle-right');

            // 标题
            $title = array_get($val, 'title', __('No Title'));

            if ($level === 1) {
                $title = "<span class='title'>{$title}</span>";
            }

            // 网址
            $url = (isset($val['url']) && $val['url']) ? url($val['url']) : 'javascript:;';

            // 子菜单
            $arrow = $submenu = '';
            if (isset($val['submenu'])) {
                $arrow = "<span class=\"arrow\"></span>";
                $submenu = sidebar_build_menu($val['submenu'], $level + 1);
                $submenu = "<ul class=\"sub-menu\">\n$submenu\n</ul>";
            }

            // 是否激活菜单选中
            // 如果子菜单存在选中，则自动选中
            $selected = '';
            if (array_get($val, 'active') || preg_match('~class=".*?active.*?"~', $submenu)) {
                if (isset($val['attribute']['class'])) {
                    $val['attribute']['class'] .= ' active';
                } else {
                    $val['attribute']['class'] = ' active';
                }

                // 一级菜单被选中
                if ($level === 1) {
                    $selected = '<span class="selected"></span>';
                }
            }

            // 属性
            $attribute = '';
            if ($attr = array_get($val, 'attribute')) {
                if (is_array($attr)) {
                    foreach ($attr as $k => $v) {
                        $attribute .= " {$k}=\"{$v}\"";
                    }
                } else {
                    $attribute = $attr;
                }
            }

            $html .= "<li {$attribute}>"
                   . "    <a href=\"{$url}\">"
                   . "        <i class=\"fa fa-{$icon}\"></i>"
                   . "        {$title}"
                   . "        {$selected}"
                   . "        {$arrow}"
                   . "    </a>"
                   . "    {$submenu}"
                   . "</li>";
        }
        return $html;
    };
}
?>

<div class="page-sidebar-wrapper">
    <div class="page-sidebar navbar-collapse collapse">
        <!-- add "navbar-no-scroll" class to disable the scrolling of the sidebar menu -->
        <!-- BEGIN SIDEBAR MENU -->
        <ul class="page-sidebar-menu" data-auto-scroll="true" data-slide-speed="200">
            <li class="sidebar-toggler-wrapper">
                <!-- BEGIN SIDEBAR TOGGLER BUTTON -->
                <div class="sidebar-toggler hidden-phone"></div>
                <!-- BEGIN SIDEBAR TOGGLER BUTTON -->
            </li>
            <li class="sidebar-search-wrapper">
                <!-- BEGIN RESPONSIVE QUICK SEARCH FORM -->
                <form class="sidebar-search" action="<?=url('customer')?>">
                    <div class="form-container">
                        <div class="input-box">
                            <a href="javascript:;" class="remove"></a>
                            <input type="text" name="mt4_live" placeholder="MT4 LOGIN ..." />
                            <input type="button" class="submit" value=" " />
                        </div>
                    </div>
                </form>
                <!-- END RESPONSIVE QUICK SEARCH FORM -->
            </li>

            <?=sidebar_build_menu(config('menu')->toArray())?>

        </ul>
        <!-- END SIDEBAR MENU -->
    </div>
</div>
